local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.ModelObject());
local minPath="ReplicatedStorage.Mine"
local ConnectClass=_G.CommonOHOH.load.Connect();
local mine_cfg=_G.CommonOHOH.config.Mine
local HttpService = game:GetService("HttpService")

 function gGetMineObject(id)
    local path= string.format("%s.%s.Model",minPath,id)
    local model= CommonOHOH:GetPath_Loop(path):Clone()
    for _,p in pairs( model:GetDescendants()) do
        if p:IsA("BasePart") then
            p.Anchored=true
            p.CanTouch=false
            p.CanCollide=false
            if p.Name=="Instance" then
                p.CanTouch=true
            end
        end
    end
    model.PrimaryPart=model:FindFirstChild("Instance")
    return model
 end

 function CreateGuid()
	return HttpService:GenerateGUID(false)
end


-- 矿石对象
function M.new(...)
	return M(...);
end;

function M.ctor(self,id)--构造函数
    self.DeadEvent=ConnectClass.new() -- 死亡
    self.HurtEvent=ConnectClass.new() -- 受伤
    self.AttackEvent=ConnectClass.new() -- 受伤
    self.Guid=CreateGuid()
    self.cfg=mine_cfg[id]
    local model=gGetMineObject(self.cfg.perfab)

    self.Hp=self.cfg.hp
    self.Damge=self.cfg.damge
	M.super.ctor(self,model);
    self.attribute.IsCarTouch=true
    self:AddPrivateComponentByFolder(script.com)
end;

function M.dtor(self)
    self.DeadEvent:dtor()
    self.HurtEvent:dtor()
    self.AttackEvent:dtor()

	M.super.dtor(self);
    self.Obj:Destroy()
    self.Obj=nil
end;


function M.GetDamge(self)
    return  self.Damge
end

function M.GetHp(self)
    return  self.Hp
end

--受伤
function M.Hurt(self,damge,attackerList)
    self.Hp=self.Hp-damge
    self.HurtEvent:Action(damge,attackerList)
    if self.Hp<=0 then
        self.Hp=0
        self:Dead()
    end
end


function M.GetGuid(self)
    return self.Guid
end

function M.Installation(self,cf,parent)
    self.main_inst:PivotTo(cf)
    self.main_inst.Parent=parent
end

function M.Dead(self)
   
    self.Obj=self.main_inst
    self:AddProcess(function()
        self.DeadEvent:Action()
        task.wait()
        self:dtor()
    end)
   
end

return M